<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var numberOfMatches = function (n) {
      let i = n, res = 0
      while (i > 1) {
        if (i % 2 == 0) {
          i /= 2
          res += i
        } else {
          res += (i - 1) / 2
          i = (i - 1) / 2 + 1
        }
      }
      return res
    };
    console.log(numberOfMatches(7))
  </script>
</body>

</html>